﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Felbot.Interface.Conversations;
using Felbot.IO;
using Felbot.Profiles;

namespace Felbot.Interface {
    /// <summary>This class represents the global configuration form, where options not available to individual profiles are customizable.</summary>
    public partial class frmGlobalConfiguration : Form {
        private Dictionary<string, FCSSetting> m_Settings = new Dictionary<string, FCSSetting>();
        private string m_CurrentKey;
        private FCSSetting m_CurrentSetting;
        private bool m_Loading = false;

        /// <summary>Creates a new global configuration form.</summary>
        public frmGlobalConfiguration() {
            InitializeComponent();
        }

        private void frmConfiguration_Load(object sender, EventArgs e) {
            LoadGlobalProfile();
        }

        private void LoadGlobalProfile() {
            tcGlobalControl.SelectTab(0);
            lblInfo.Text = "Felbot start-up settings: how does it start?";

            //start tab
            cbStartWithWindows.Checked = ProfileControl.Global.GetSetting("StartUp", "LoadWithWindows").ToBoolean(false);
            #region PositionChoice
            switch (ProfileControl.Global.GetSetting("StartUp", "PositionChoice").ToInteger(0)) {
                case 0:
                default: //default
                    rbStartPosDefault.Checked = true;
                    break;
                case 1: //store
                    rbStartPosStore.Checked = true;
                    break;
                case 2: //custom
                    rbStartPosCustom.Checked = true;
                    break;
            }
            #endregion
            nudStartPosW.Value = (decimal) ProfileControl.Global.GetSetting("StartUp", "PositionW").ToInteger(800);
            nudStartPosH.Value = (decimal) ProfileControl.Global.GetSetting("StartUp", "PositionH").ToInteger(600);
            nudStartPosL.Value = (decimal) ProfileControl.Global.GetSetting("StartUp", "PositionL").ToInteger(200);
            nudStartPosT.Value = (decimal) ProfileControl.Global.GetSetting("StartUp", "PositionT").ToInteger(200);
            cboStartState.SelectedIndex = ProfileControl.Global.GetSetting("StartUp", "State").ToInteger(0);
            cboStartStyle.SelectedIndex = ProfileControl.Global.GetSetting("StartUp", "StyleChoice").ToInteger(0);

            //fonts, colors, and styles tab
            LoadFCSList();

            //interface
            cbRtbMaxlines.Checked = ProfileControl.Global.GetSetting("Interface", "RTBMaxLines").ToBoolean(true);
            nudRtbMaxlines.Value = (decimal) ProfileControl.Global.GetSetting("Interface", "RTBMaxLinesNumber").ToInteger(500);
            cbRtbMaxchars.Checked = ProfileControl.Global.GetSetting("Interface", "RTBMaxChars").ToBoolean(false);
            nudRtbMaxchars.Value = (decimal) ProfileControl.Global.GetSetting("Interface", "RTBMaxCharsNumber").ToInteger(1000);
            cbRtbUrls.Checked = ProfileControl.Global.GetSetting("Interface", "RTBURLDetection").ToBoolean(true);
            cbRtbSearch.Checked = ProfileControl.Global.GetSetting("Interface", "RTBInLineSearch").ToBoolean(true);
            cboIfacePerspective.SelectedIndex = ProfileControl.Global.GetSetting("Interface", "Perspective").ToInteger(0);
            txtIfaceD2Style1.Text = ProfileControl.Global.GetSetting("Interface", "D2StyleWithChar").ToString("%c (*%u)");
            txtIfaceD2Style2.Text = ProfileControl.Global.GetSetting("Interface", "D2StyleWithoutChar").ToString("*%u");
            cbIfaceComplete.Checked = ProfileControl.Global.GetSetting("Interface", "AutoComplete").ToBoolean(true);
            cbIfaceComplChannel.Checked = ProfileControl.Global.GetSetting("Interface", "AutoCompleteChannel").ToBoolean(true);
            cbIfaceComplFriends.Checked = ProfileControl.Global.GetSetting("Interface", "AutoCompleteFriends").ToBoolean(false);
            cbIfaceComplClan.Checked = ProfileControl.Global.GetSetting("Interface", "AutoCompleteClan").ToBoolean(false);
            cbIfaceRecent.Checked = ProfileControl.Global.GetSetting("Interface", "AutoCompleteRecent").ToBoolean(true);
            nudIfaceRecent.Value = (decimal) ProfileControl.Global.GetSetting("Interface", "AutoCompleteRecentNumber").ToInteger(15);
            cbIfaceQuickWhisperReplace.Checked = ProfileControl.Global.GetSetting("Interface", "QuickWhisperReplace").ToBoolean(true);
            cbIfaceChannelJLMsgs.Checked = ProfileControl.Global.GetSetting("Interface", "ShowJoinLeaveMessages").ToBoolean(true);
            cbIfaceChannelUICMsgs.Checked = ProfileControl.Global.GetSetting("Interface", "ShowUserInChannelMessages").ToBoolean(false);
            cboIfaceChannelOrderUsers.SelectedIndex = ProfileControl.Global.GetSetting("Interface", "OrderChannel").ToInteger(0);
            cboIfaceChannelOrderClan.SelectedIndex = ProfileControl.Global.GetSetting("Interface", "OrderClan").ToInteger(0);
            cbIfaceChannelReorderFriends.Checked = ProfileControl.Global.GetSetting("Interface", "ReorderFriends").ToBoolean(true);

            //interaction
            cbIactionScriptsEnable.Checked = ProfileControl.Global.GetSetting("Interaction", "ScriptsEnabled").ToBoolean(true);
            cbIactionScriptsStartOutput.Checked = ProfileControl.Global.GetSetting("Interaction", "ScriptsStartOutput").ToBoolean(false);
            cbIactionCmdsEnable.Checked = ProfileControl.Global.GetSetting("Interaction", "CommandsEnabled").ToBoolean(true);
            cbIactionCmdsWhisperResponse.Checked = ProfileControl.Global.GetSetting("Interaction", "CommandsWhisperResponse").ToBoolean(false);
            cbIactionCmdsTriggerlessWhisper.Checked = ProfileControl.Global.GetSetting("Interaction", "CommandsTriggerlessWhisper").ToBoolean(false);
            cbIactionCmdsTrigProfile.Checked = ProfileControl.Global.GetSetting("Interaction", "CommandsTriggerProfile").ToBoolean(true);
            cbIactionCmdsTrigGlobal.Checked = ProfileControl.Global.GetSetting("Interaction", "CommandsTriggerGlobal").ToBoolean(true);
            cbIactionCmdsTrigUsername.Checked = ProfileControl.Global.GetSetting("Interaction", "CommandsTriggerUsername").ToBoolean(true);
            cbIactionCmdsTrigOps.Checked = ProfileControl.Global.GetSetting("Interaction", "CommandsTriggerOps").ToBoolean(true);
            cbIactionCmdsTrigBots.Checked = ProfileControl.Global.GetSetting("Interaction", "CommandsTriggerBots").ToBoolean(true);
            cbIactionCmdsCrossProfile.Checked = ProfileControl.Global.GetSetting("Interaction", "CommandsCrossProfile").ToBoolean(false);

            //automation
            cboAutoConnConnected.SelectedIndex = ProfileControl.Global.GetSetting("Automation", "ConnectionWhenConnected").ToInteger(0);
            txtAutoConnConnectedChannel.Text = ProfileControl.Global.GetSetting("Automation", "ConnectionWhenConnectedChannel").ToString("");
            cbAutoConnNews.Checked = ProfileControl.Global.GetSetting("Automation", "WhenConnectedShowNews").ToBoolean(false);
            cboAutoChanKicked.SelectedIndex = ProfileControl.Global.GetSetting("Automation", "ChannelWhenKicked").ToInteger(0);
            nudAutoChanKickedDelay.Value = (decimal) ProfileControl.Global.GetSetting("Automation", "ChannelWhenKickedDelay").ToInteger(0);
            txtAutoChanKickedChannel.Text = ProfileControl.Global.GetSetting("Automation", "ChannelWhenKickedChannel").ToString("");
            cboAutoChanBanned.SelectedIndex = ProfileControl.Global.GetSetting("Automation", "ChannelWhenBanned").ToInteger(0);
            nudAutoChanBannedDelay.Value = (decimal) ProfileControl.Global.GetSetting("Automation", "ChannelWhenBannedDelay").ToInteger(15);
            txtAutoChanBannedChannel.Text = ProfileControl.Global.GetSetting("Automation", "ChannelWhenBannedChannel").ToString("");
            cboAutoChanInvited.SelectedIndex = ProfileControl.Global.GetSetting("Automation", "ChannelWhenInvited").ToInteger(0);
            txtAutoClanInvitedTag.Text = ProfileControl.Global.GetSetting("Automation", "ClanWhenInvitedTag").ToString("");
            cbAutoChanNoQueue.Checked = ProfileControl.Global.GetSetting("Automation", "DisableQueue").ToBoolean(false);
            #region LogChoice
            switch (ProfileControl.Global.GetSetting("Automation", "Logging").ToInteger(0)) {
                case 2:
                default: //default - RTF
                    rbAutoLogRtf.Checked = true;
                    break;
                case 1: //TXT
                    rbAutoLogTxt.Checked = true;
                    break;
                case 0: //none
                    rbAutoLogNo.Checked = true;
                    break;
            }
            #endregion
            cbAutoLogDelOld.Checked = ProfileControl.Global.GetSetting("Automation", "LoggingDeleteOld").ToBoolean(false);
            nudAutoLogDelOld.Value = (decimal) ProfileControl.Global.GetSetting("Automation", "LoggingDeleteOld").ToInteger(1);
            cbAutoLogScript.Checked = ProfileControl.Global.GetSetting("Automation", "LoggingLogScript").ToBoolean(true);
            cbAutoLogCommand.Checked = ProfileControl.Global.GetSetting("Automation", "LoggingLogCommands").ToBoolean(false);

            btnOK.Enabled = false;
        }

        private void SaveGlobalProfile() {
            btnOK.Enabled = false;

            //start tab
            ProfileControl.Global.SetSetting("StartUp", "LoadWithWindows", cbStartWithWindows.Checked);
            SetWindowsStartup(cbStartWithWindows.Checked);
            #region PositionChoice
            if (rbStartPosCustom.Checked)
                ProfileControl.Global.SetSetting("StartUp", "PositionChoice", 2);
            else if (rbStartPosStore.Checked)
                ProfileControl.Global.SetSetting("StartUp", "PositionChoice", 1);
            else
                ProfileControl.Global.SetSetting("StartUp", "PositionChoice", 0);
            #endregion
            ProfileControl.Global.SetSetting("StartUp", "PositionW", (int) nudStartPosW.Value);
            ProfileControl.Global.SetSetting("StartUp", "PositionH", (int) nudStartPosH.Value);
            ProfileControl.Global.SetSetting("StartUp", "PositionL", (int) nudStartPosL.Value);
            ProfileControl.Global.SetSetting("StartUp", "PositionT", (int) nudStartPosT.Value);
            ProfileControl.Global.SetSetting("StartUp", "State", cboStartState.SelectedIndex);
            ProfileControl.Global.SetSetting("StartUp", "StyleChoice", cboStartStyle.SelectedIndex);

            //fonts, colors, and styles tab
            SaveFCSList();

            //interface
            ProfileControl.Global.SetSetting("Interface", "RTBMaxLines", cbRtbMaxlines.Checked);
            ProfileControl.Global.SetSetting("Interface", "RTBMaxLinesNumber", (int) nudRtbMaxlines.Value);
            ProfileControl.Global.SetSetting("Interface", "RTBMaxChars", cbRtbMaxchars.Checked);
            ProfileControl.Global.SetSetting("Interface", "RTBMaxCharsNumber", (int) nudRtbMaxchars.Value);
            ProfileControl.Global.SetSetting("Interface", "RTBURLDetection", cbRtbUrls.Checked);
            ProfileControl.Global.SetSetting("Interface", "RTBInLineSearch", cbRtbSearch.Checked);
            ProfileControl.Global.SetSetting("Interface", "Perspective", cboIfacePerspective.SelectedIndex);
            ProfileControl.Global.SetSetting("Interface", "D2StyleWithChar", txtIfaceD2Style1.Text);
            ProfileControl.Global.SetSetting("Interface", "D2StyleWithoutChar", txtIfaceD2Style2.Text);
            ProfileControl.Global.SetSetting("Interface", "AutoComplete", cbIfaceComplete.Checked);
            ProfileControl.Global.SetSetting("Interface", "AutoCompleteChannel", cbIfaceComplChannel.Checked);
            ProfileControl.Global.SetSetting("Interface", "AutoCompleteFriends", cbIfaceComplFriends.Checked);
            ProfileControl.Global.SetSetting("Interface", "AutoCompleteClan", cbIfaceComplClan.Checked);
            ProfileControl.Global.SetSetting("Interface", "AutoCompleteRecent", cbIfaceRecent.Checked);
            ProfileControl.Global.SetSetting("Interface", "AutoCompleteRecentNumber", (int) nudIfaceRecent.Value);
            ProfileControl.Global.SetSetting("Interface", "QuickWhisperReplace", cbIfaceQuickWhisperReplace.Checked);
            ProfileControl.Global.SetSetting("Interface", "ShowJoinLeaveMessages", cbIfaceChannelJLMsgs.Checked);
            ProfileControl.Global.SetSetting("Interface", "ShowUserInChannelMessages", cbIfaceChannelUICMsgs.Checked);
            ProfileControl.Global.SetSetting("Interface", "OrderChannel", cboIfaceChannelOrderUsers.SelectedIndex);
            ProfileControl.Global.SetSetting("Interface", "OrderClan", cboIfaceChannelOrderClan.SelectedIndex);
            ProfileControl.Global.SetSetting("Interface", "ReorderFriends", cbIfaceChannelReorderFriends.Checked);

            //interaction
            ProfileControl.Global.SetSetting("Interaction", "ScriptsEnabled", cbIactionScriptsEnable.Checked);
            ProfileControl.Global.SetSetting("Interaction", "ScriptsStartOutput", cbIactionScriptsStartOutput.Checked);
            ProfileControl.Global.SetSetting("Interaction", "CommandsEnabled", cbIactionCmdsEnable.Checked);
            ProfileControl.Global.SetSetting("Interaction", "CommandsWhisperResponse", cbIactionCmdsWhisperResponse.Checked);
            ProfileControl.Global.SetSetting("Interaction", "CommandsTriggerlessWhisper", cbIactionCmdsTriggerlessWhisper.Checked);
            ProfileControl.Global.SetSetting("Interaction", "CommandsTriggerProfile", cbIactionCmdsTrigProfile.Checked);
            ProfileControl.Global.SetSetting("Interaction", "CommandsTriggerGlobal", cbIactionCmdsTrigGlobal.Checked);
            ProfileControl.Global.SetSetting("Interaction", "CommandsTriggerUsername", cbIactionCmdsTrigUsername.Checked);
            ProfileControl.Global.SetSetting("Interaction", "CommandsTriggerOps", cbIactionCmdsTrigOps.Checked);
            ProfileControl.Global.SetSetting("Interaction", "CommandsTriggerBots", cbIactionCmdsTrigBots.Checked);
            ProfileControl.Global.SetSetting("Interaction", "CommandsCrossProfile", cbIactionCmdsCrossProfile.Checked);

            //automation
            ProfileControl.Global.SetSetting("Automation", "ConnectionWhenConnected", cboAutoConnConnected.SelectedIndex);
            ProfileControl.Global.SetSetting("Automation", "ConnectionWhenConnectedChannel", txtAutoConnConnectedChannel.Text);
            ProfileControl.Global.SetSetting("Automation", "WhenConnectedShowNews", cbAutoConnNews.Checked);
            ProfileControl.Global.SetSetting("Automation", "ChannelWhenKicked", cboAutoChanKicked.SelectedIndex);
            ProfileControl.Global.SetSetting("Automation", "ChannelWhenKickedDelay", (int) nudAutoChanKickedDelay.Value);
            ProfileControl.Global.SetSetting("Automation", "ChannelWhenKickedChannel", txtAutoChanKickedChannel.Text);
            ProfileControl.Global.SetSetting("Automation", "ChannelWhenBanned", cboAutoChanBanned.SelectedIndex);
            ProfileControl.Global.SetSetting("Automation", "ChannelWhenBannedDelay", (int) nudAutoChanBannedDelay.Value);
            ProfileControl.Global.SetSetting("Automation", "ChannelWhenBannedChannel", txtAutoChanBannedChannel.Text);
            ProfileControl.Global.SetSetting("Automation", "ChannelWhenInvited", cboAutoChanInvited.SelectedIndex);
            ProfileControl.Global.SetSetting("Automation", "ClanWhenInvitedTag", txtAutoClanInvitedTag.Text);
            ProfileControl.Global.SetSetting("Automation", "DisableQueue", cbAutoChanNoQueue.Checked);
            #region LogChoice
            if (rbAutoLogNo.Checked)
                ProfileControl.Global.SetSetting("Automation", "Logging", 0);
            else if (rbAutoLogTxt.Checked)
                ProfileControl.Global.SetSetting("Automation", "Logging", 1);
            else
                ProfileControl.Global.SetSetting("Automation", "Logging", 2);
            #endregion
            ProfileControl.Global.SetSetting("Automation", "LoggingDeleteOld", cbAutoLogDelOld.Checked);
            ProfileControl.Global.SetSetting("Automation", "LoggingDeleteOld", (int) nudAutoLogDelOld.Value);
            ProfileControl.Global.SetSetting("Automation", "LoggingLogScript", cbAutoLogScript.Checked);
            ProfileControl.Global.SetSetting("Automation", "LoggingLogCommands", cbAutoLogCommand.Checked);

            //save to file
            ProfileControl.Global.SaveSettings();
        }

        private void LoadFCSList() {
            ListViewGroup chatroom = new ListViewGroup("Chatroom");
            ListViewGroup userlists = new ListViewGroup("Userlists");
            lvFCS.Groups.AddRange(new ListViewGroup[2] { chatroom, userlists });
            lvFCS.Items.AddRange(new ListViewItem[] {
                GetFCSListItem(chatroom, "Text"),
                GetFCSListItem(chatroom, "Timestamp"),
                GetFCSListItem(chatroom, "Bot: Try"),
                GetFCSListItem(chatroom, "Bot: Success"),
                GetFCSListItem(chatroom, "Bot: Fail"),
                GetFCSListItem(chatroom, "Information: Info"),
                GetFCSListItem(chatroom, "Information: Error"),
                GetFCSListItem(chatroom, "Information: Username"),
                GetFCSListItem(chatroom, "Information: Status"),
                GetFCSListItem(chatroom, "Information: Clan"),
                GetFCSListItem(chatroom, "Information: Command"),
                GetFCSListItem(chatroom, "Talk: Normal Text"),
                GetFCSListItem(chatroom, "Talk: Emoted Text"),
                GetFCSListItem(chatroom, "Talk: Whispered To Text"),
                GetFCSListItem(chatroom, "Talk: Whispered From Text"),
                GetFCSListItem(chatroom, "Talk: User Name"),
                GetFCSListItem(chatroom, "Talk: Emoted Name"),
                GetFCSListItem(chatroom, "Talk: Whispered To Name"),
                GetFCSListItem(chatroom, "Talk: Whispered From Name"),
                GetFCSListItem(chatroom, "Talk: Operator Name"),
                GetFCSListItem(chatroom, "Talk: Representative Name"),
                GetFCSListItem(chatroom, "Talk: Invisible Name"),
                GetFCSListItem(chatroom, "Talk: Your Name"),
                GetFCSListItem(chatroom, "Talk: Watched Name"),
                GetFCSListItem(userlists, "Header"),
                GetFCSListItem(userlists, "Item"),
                GetFCSListItem(userlists, "Channel: Normal"),
                GetFCSListItem(userlists, "Channel: Operator"),
                GetFCSListItem(userlists, "Channel: Representative"),
                GetFCSListItem(userlists, "Channel: Invisible"),
                GetFCSListItem(userlists, "Channel: Squelched"),
                GetFCSListItem(userlists, "Channel: You"),
                GetFCSListItem(userlists, "Channel: Watched"),
                GetFCSListItem(userlists, "Channel: Idle"),
                GetFCSListItem(userlists, "Friend: Online"),
                GetFCSListItem(userlists, "Friend: Offline"),
                GetFCSListItem(userlists, "Friend: Mutual"),
                GetFCSListItem(userlists, "Clan: Online"),
                GetFCSListItem(userlists, "Clan: Offline"),
                GetFCSListItem(userlists, "Clan: You")
            });
        }

        private void SaveFCSList() {
            foreach (KeyValuePair<string, FCSSetting> kvp in m_Settings) {
                ProfileControl.Global.SetSetting("FontColorStyle", kvp.Key, kvp.Value);
            }
        }

        /// <summary>
        /// Called during FCS load, gets a list view item and stores the setting in the dictionary.
        /// </summary>
        private ListViewItem GetFCSListItem(ListViewGroup group, string text) {
            string key = group.Header + "_" + text.Replace(':', '_').Replace(" ", "");
            FCSSetting fcs = ProfileControl.Global.GetSetting("FontColorStyle", key).ToFCSSetting(FCSControl.GetDefaultSetting(key));
            ListViewItem lvi = new ListViewItem();
            lvi.Text = text;
            if (fcs.FontFamily != null)
                lvi.Font = new Font(new FontFamily(fcs.FontFamily), lvi.Font.Size);
            lvi.Font = new Font(lvi.Font, fcs.FontStyle);
            lvi.ForeColor = Color.White;
            if (!fcs.ColorFore.IsEmpty && fcs.ColorFore != Color.Transparent)
                lvi.ForeColor = fcs.ColorFore;
            lvi.BackColor = Color.Black;
            if (!fcs.ColorBack.IsEmpty && fcs.ColorBack != Color.Transparent)
                lvi.BackColor = fcs.ColorBack;
            lvi.Group = group;
            lvi.Name = key;
            m_Settings.Add(key, fcs);
            return lvi;
        }

        /// <summary>
        /// Called during FCS item save, gets a list view item and stores the new setting in the dictionary.
        /// </summary>
        private ListViewItem GetFCSListItem(string key, FCSSetting value) {
            ListViewItem lvi = new ListViewItem();
            if (value.FontFamily != null)
                lvi.Font = new Font(new FontFamily(value.FontFamily), lvi.Font.Size);
            lvi.Font = new Font(lvi.Font, value.FontStyle);
            lvi.ForeColor = Color.White;
            if (!value.ColorFore.IsEmpty && value.ColorFore != Color.Transparent)
                lvi.ForeColor = value.ColorFore;
            lvi.BackColor = Color.Black;
            if (!value.ColorBack.IsEmpty && value.ColorBack != Color.Transparent)
                lvi.BackColor = value.ColorBack;
            lvi.Group = lvFCS.Groups[key.Split('_')[0]];
            m_Settings[key] = value;
            return lvi;
        }

        private void btnFCSDefault_Click(object sender, EventArgs e) {
            if (MessageBox.Show("Are you sure you want to reset this setting to default?" +
                                "This action is perminant.",
                                "Felbot",
                                MessageBoxButtons.YesNo,
                                MessageBoxIcon.Question,
                                MessageBoxDefaultButton.Button2)
                    == DialogResult.Yes) {
                control_Changed(sender, e);
                lvFCS.Items.Find(m_CurrentKey, false)[0] =
                    GetFCSListItem(m_CurrentKey, FCSControl.GetDefaultSetting(m_CurrentKey));
            }
        }

        private void btnFCSSave_Click(object sender, EventArgs e) {
            control_Changed(sender, e);
            lvFCS.Items.Find(m_CurrentKey, false)[0] =
                GetFCSListItem(m_CurrentKey, m_CurrentSetting);
            lvFCS.SelectedItems.Clear();
            btnOK.Enabled = true;
        }

        private void btnFCSDiscard_Click(object sender, EventArgs e) {
            lvFCS.SelectedItems.Clear();
        }

        private void lvFCS_SelectedIndexChanged(object sender, EventArgs e) {
            if (lvFCS.SelectedItems.Count == 1) {
                m_Loading = true;
                m_CurrentKey = lvFCS.SelectedItems[0].Name;
                m_CurrentSetting = m_Settings[lvFCS.SelectedItems[0].Name];
                if (m_CurrentSetting.FontFamily != null)
                    cboFCSFontFamily.SelectedFontFamily = new FontFamily(m_CurrentSetting.FontFamily);
                else
                    cboFCSFontFamily.SelectedFontFamily = null;
                cbFCSFontBold.Checked = ((m_CurrentSetting.FontStyle & FontStyle.Bold) == FontStyle.Bold);
                cbFCSFontItal.Checked = ((m_CurrentSetting.FontStyle & FontStyle.Italic) == FontStyle.Italic);
                cbFCSFontLine.Checked = ((m_CurrentSetting.FontStyle & FontStyle.Underline) == FontStyle.Underline);
                cbFCSFontStrike.Checked = ((m_CurrentSetting.FontStyle & FontStyle.Underline) == FontStyle.Underline);
                cboFCSColorFG.SelectedColor = m_CurrentSetting.ColorFore;
                cboFCSColorBG.SelectedColor = m_CurrentSetting.ColorBack;
                cboFCSStyle.SelectedIndex = (int) m_CurrentSetting.StyleType;
                string styleCustom = m_CurrentSetting.StyleCustom;
                if (styleCustom != null && styleCustom.Contains("%t")) {
                    txtFCSStyle1.Text = styleCustom.Substring(0, styleCustom.IndexOf("%t"));
                    txtFCSStyle2.Text = styleCustom.Substring(styleCustom.IndexOf("%t") + 2, styleCustom.Length - styleCustom.IndexOf("%t") + 2);
                } else {
                    txtFCSStyle1.Text = "";
                    txtFCSStyle2.Text = "";
                }
                grpFCS.Show();
                btnFCSDefault.Show();
                btnFCSSave.Show();
                btnFCSDiscard.Show();
                m_Loading = false;
            } else {
                grpFCS.Hide();
                btnFCSDefault.Hide();
                btnFCSSave.Hide();
                btnFCSDiscard.Hide();
                m_CurrentKey = null;
                m_CurrentSetting = null;
            }
        }

        private void SetWindowsStartup(bool set) {
            FileInfo startupLink = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "Felbot.lnk");
            FileInfo programsLink = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "Felbot\\Felbot.lnk");
            if (set) {
                if (programsLink.Exists) {
                    if (!startupLink.Directory.Exists) {
                        Directory.CreateDirectory(startupLink.DirectoryName);
                    }

                    programsLink.CopyTo(startupLink.FullName, true);
                } else {
                    MessageBox.Show("Felbot cannot start on Windows start without the Start Menu link.",
                                    "Felbot",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    ProfileControl.Global.SetSetting("StartUp", "LoadWithWindows", false);
                }
            } else {
                if (startupLink.Exists) {
                    startupLink.Delete();
                }
            }
                    
        }

        private void btnOK_Click(object sender, EventArgs e) {
            SaveGlobalProfile();
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void control_Enter(object sender, EventArgs e) {
            lblInfo.Text = tip.GetToolTip((Control) sender);
        }

        private void control_Changed(object sender, EventArgs e) {
            btnOK.Enabled = true;
        }

        private void tcGlobalControl_Selected(object sender, TabControlEventArgs e) {
            switch (e.TabPageIndex) {
                case 0: //start
                    lblInfo.Text = "Felbot start-up settings: how does it start?";
                    break;
                case 1: //fonts, colors, and styles
                    lblInfo.Text = "Felbot fonts, colors, and styles settings: fine-tune your life!";
                    break;
                case 2: //interface
                    lblInfo.Text = "Felbot interface settings: more on how it looks (and feels).";
                    break;
                case 3: //interaction
                    lblInfo.Text = "Felbot intaraction settings: how you interact with it.";
                    break;
                case 4: //automation
                    lblInfo.Text = "Felbot automation settings: it's not a bot unless it's autonomous.";
                    break;
            }
        }

        //tab: start

        private void rbStartPosDefault_CheckedChanged(object sender, EventArgs e) {
            bool checkedValue = rbStartPosCustom.Checked;
            lblStartPosWH.Visible = checkedValue;
            nudStartPosW.Visible = checkedValue;
            lblStartPosWHx.Visible = checkedValue;
            nudStartPosH.Visible = checkedValue;
            lblStartPosLT.Visible = checkedValue;
            nudStartPosL.Visible = checkedValue;
            lblStartPosLTc.Visible = checkedValue;
            nudStartPosT.Visible = checkedValue;
        }

        //tab: fonts, colors, and styles

        private void cboFCSStyle_SelectedIndexChanged(object sender, EventArgs e) {
            if (!m_Loading) {
                switch (cboFCSStyle.SelectedIndex) {
                    case 0: //default
                        m_CurrentSetting.StyleType = FCSStyle.Default;
                        txtFCSStyle1.Hide();
                        txtFCSStyle2.Hide();
                        break;
                    case 1: //hidden
                        m_CurrentSetting.StyleType = FCSStyle.Hidden;
                        txtFCSStyle1.Hide();
                        txtFCSStyle2.Hide();
                        break;
                    case 2: //custom
                        m_CurrentSetting.StyleType = FCSStyle.Custom;
                        txtFCSStyle1.Show();
                        txtFCSStyle2.Show();
                        break;
                }
            }
        }

        private void cbFCSFontStyle_CheckedChanged(object sender, EventArgs e) {
            if (!m_Loading) {
                int m_iFCSFontStyle = 0;
                if (cbFCSFontBold.Checked)
                    m_iFCSFontStyle++;
                if (cbFCSFontItal.Checked)
                    m_iFCSFontStyle += 2;
                if (cbFCSFontLine.Checked)
                    m_iFCSFontStyle += 4;
                if (cbFCSFontStrike.Checked)
                    m_iFCSFontStyle += 8;
                m_CurrentSetting.FontStyle = (FontStyle) m_iFCSFontStyle;
            }
        }

        //tab: interface

        private void cbRtbMaxlines_CheckedChanged(object sender, EventArgs e) {
            nudRtbMaxlines.Visible = cbRtbMaxlines.Checked;
        }

        private void cbRtbMaxchars_CheckedChanged(object sender, EventArgs e) {
            nudRtbMaxchars.Visible = cbRtbMaxchars.Checked;
        }

        private void cbIfaceComplete_CheckedChanged(object sender, EventArgs e) {
            grpIfaceComplete.Enabled = cbIfaceComplete.Checked;
        }

        private void cbIfaceRecent_CheckedChanged(object sender, EventArgs e) {
            bool isChecked = cbIfaceRecent.Checked;
            lblIfaceRecentStore.Visible = isChecked;
            nudIfaceRecent.Visible = isChecked;
            lblIfaceRecentEntries.Visible = isChecked;
        }

        //tab: interaction

        private void cbIactionScriptsEnable_CheckedChanged(object sender, EventArgs e) {
            bool isChecked = cbIactionScriptsEnable.Checked;
            cbIactionScriptsStartOutput.Enabled = isChecked;
            cbIactionCmdsEnable.Enabled = isChecked;
            cbIactionCmdsWhisperResponse.Enabled = isChecked;
            cbIactionCmdsTriggerlessWhisper.Enabled = isChecked;
            cbIactionCmdsTrigProfile.Enabled = isChecked;
            cbIactionCmdsTrigGlobal.Enabled = isChecked;
            cbIactionCmdsTrigUsername.Enabled = isChecked;
            cbIactionCmdsTrigOps.Enabled = isChecked;
            cbIactionCmdsTrigBots.Enabled = isChecked;
            cbIactionCmdsCrossProfile.Enabled = isChecked;
            btnIactionCmdsManage.Enabled = isChecked;
        }

        private void cbIactionCmdsEnable_CheckedChanged(object sender, EventArgs e) {
            bool isChecked = cbIactionCmdsEnable.Checked;
            cbIactionCmdsWhisperResponse.Enabled = isChecked;
            cbIactionCmdsTriggerlessWhisper.Enabled = isChecked;
            cbIactionCmdsTrigProfile.Enabled = isChecked;
            cbIactionCmdsTrigGlobal.Enabled = isChecked;
            cbIactionCmdsTrigUsername.Enabled = isChecked;
            cbIactionCmdsTrigOps.Enabled = isChecked;
            cbIactionCmdsTrigBots.Enabled = isChecked;
            cbIactionCmdsCrossProfile.Enabled = isChecked;
            btnIactionCmdsManage.Enabled = isChecked;
        }

        //tab: automation

        private void cboAutoConnConnected_SelectedIndexChanged(object sender, EventArgs e) {
            control_Changed(sender, e);
            txtAutoConnConnectedChannel.Visible = (cboAutoConnConnected.SelectedIndex == 2);
        }

        private void cboAutoChanKicked_SelectedIndexChanged(object sender, EventArgs e) {
            control_Changed(sender, e);
            nudAutoChanKickedDelay.Visible = (cboAutoChanKicked.SelectedIndex == 1);
            txtAutoChanKickedChannel.Visible = (cboAutoChanKicked.SelectedIndex == 3);
        }

        private void cboAutoChanBanned_SelectedIndexChanged(object sender, EventArgs e) {
            control_Changed(sender, e);
            txtAutoChanBannedChannel.Visible = (cboAutoChanBanned.SelectedIndex == 2);
            nudAutoChanBannedDelay.Visible = (cboAutoChanBanned.SelectedIndex == 3);
        }

        private void cboAutoChanInvited_SelectedIndexChanged(object sender, EventArgs e) {
            control_Changed(sender, e);
            txtAutoClanInvitedTag.Visible = (cboAutoChanInvited.SelectedIndex == 4);
        }

        private void rbAutoLog_CheckedChanged(object sender, EventArgs e) {
            control_Changed(sender, e);
            bool isChecked = rbAutoLogNo.Checked;
            cbAutoLogDelOld.Enabled = isChecked;
            nudAutoLogDelOld.Enabled = isChecked;
            lblAutoLogDelOldDays.Enabled = isChecked;
            grpAutoLogSpecial.Enabled = isChecked;
        }

        private void cbAutoLogDelOld_CheckedChanged(object sender, EventArgs e) {
            bool isChecked = cbAutoLogDelOld.Checked;
            nudAutoLogDelOld.Visible = isChecked;
            lblAutoLogDelOldDays.Visible = isChecked;
        }
    }
}
